package com.czl.module_storehouse.mvp.model;

import com.czl.base.config.AppConstants;
import com.czl.base.data.net.RetrofitClient;
import com.czl.base.util.SpHelper;
import com.czl.module_base.bean.CompanyBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.model.BaseModel;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.api.ApiService;
import com.czl.module_storehouse.bean.InventoryInfoBean;
import com.czl.module_storehouse.bean.PurchaseBean;
import com.czl.module_storehouse.bean.WorkGroupBean;
import com.czl.module_storehouse.bean.WorkerBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author by xubin,
 * @date on 2022/2/11.
 * PS: Not easy to write code, please indicate.
 */
public class CompanyModel extends BasicDataModel {

    public Observable<HttpResponse<List<CompanyBean>>> selectCompanyList() {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", getManageCompanyId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).selectCompanyList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<CompanyBean>>> selectCrmCompanyByTag() {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", getManageCompanyId());
            object.put("companyTag", Constants.CompanyTag.TAG_SUPPLIER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).selectCrmCompanyByTag(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<WorkGroupBean>>> selectOrganization() {
        return selectOrganization(String.valueOf(getCompanyId()));
    }

    public Observable<HttpResponse<ListBean<WorkGroupBean>>> selectOrganization(String companyId) {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", companyId);
            object.put("userId", getUserId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class).selectOrganization(createBody(object));
    }

    public Observable<HttpResponse<ListBean<WorkerBean>>> selectUsersByCompanyId(String organizationId) {
        return selectUsersByCompanyId(String.valueOf(getCompanyId()), organizationId);
    }

    public Observable<HttpResponse<ListBean<WorkerBean>>> selectUsersByCompanyId(String companyId, String organizationId) {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", companyId);
            object.put("organizationId", organizationId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class).selectUsersByCompanyId(createBody(object));
    }
}
